package cn.cs.yangtai.service.impl;

import cn.cs.yangtai.entity.Blog;
import cn.cs.yangtai.entity.User;
import cn.cs.yangtai.entity.vo.PageResult;
import cn.cs.yangtai.mapper.UserMapper;
import cn.cs.yangtai.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2024-12-12 09:04:15
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param userid 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer userid) {
        return this.userMapper.queryById(userid);
    }

    @Override
    public User queryByPhone(String phone) {
        return userMapper.queryByPhone(phone);
    }

    /**
     * 分页查询
     *
     * @param cuttentPage 本页
     * @param pageSize    分页大小
     * @return 查询结果
     */
    @Override
    public PageResult<User> queryByPage(Integer cuttentPage, Integer pageSize) {
        PageHelper.startPage(cuttentPage,pageSize);
        Page<User> users = (Page<User>) userMapper.queryAllByLimit(new User());
        return PageResult.restPage(users);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        this.userMapper.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.userMapper.update(user);
        return this.queryById(user.getUserid());
    }

    /**
     * 通过主键删除数据
     *
     * @param userid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userid) {
        return this.userMapper.deleteById(userid) > 0;
    }


    // 检查邮箱是否已存在
    @Override
    public boolean isEmailExist(String email) {
        User user = new User();
        user.setEmail(email);
        List<User> list = userMapper.queryAllByLimit(user);
        System.out.println("邮箱已存在");
        return list.size() > 0;
    }
}
